package com.boarsoft.boar.app.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.boarsoft.agent.AgentService;
import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.app.AppInstBiz;
import com.boarsoft.boar.entity.AppInst;
import com.boarsoft.cache.redis.RedisNodeAgent;

@Component("redisAppHandler")
public class RedisAppHandler extends ShellAppHandler {
	private static final Logger log = LoggerFactory.getLogger(RedisAppHandler.class);
	@Autowired
	@Lazy
	protected RedisNodeAgent redisNodeAgent;
	@Autowired
	@Lazy
	@Qualifier("agentX")
	protected AgentService agentX;
	@Autowired
	protected AppInstBiz appInstBiz;

	@Override
	public short test(String addr) {
		if (!this.exists(addr)) {
			return AppInst.STATUS_MISSING;
		}
		try {
			String[] a = addr.split(":");
			ReplyInfo<Object> ri = redisNodeAgent.getInfo(a[0], Integer.parseInt(a[1]));
			if (ri.isSuccess()) {
				return AppInst.STATUS_RUNNING;
			} else {
				log.error("Can not get info from redis {}, reason: ", addr, ri.getData());
				return AppInst.STATUS_STOPPED;
			}
		} catch (Exception e) {
			log.error("Error on get info from redis node {}", addr, e);
			return AppInst.STATUS_STOPPED;
		}
	}
}